package com.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.PageBean;
import com.woniu.entity.Student;
import com.woniu.mapper.MyBatisUtil;
import com.woniu.mapper.StudentMapper;
import com.woniu.service.StudentService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class StudentServiceImpl implements StudentService {
    @Override
    public PageBean<Student> getStudentByPage(Student student, int pageIndex, int pageSize) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        PageBean<Student> pageBean = new PageBean<>();
        Integer i = studentMapper.selectConditionCount(student);
        List<Student> list = studentMapper.selectConditionData(student, pageIndex, pageSize);
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalRecord(i);
        pageBean.setTotalPage(i%pageSize==0?i/pageSize:i/pageSize+1);
        pageBean.setData(list);
        return pageBean;
    }

    @Override
    public PageBean<Student> Plugin(Student student,int pageIndex, int pageSize) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        PageBean<Student> pageBean = new PageBean<>();
        PageHelper.startPage(pageIndex,pageSize);
        List<Student> students = studentMapper.selectByNamePlugin(student);
        PageInfo<Student> pageInfo = new PageInfo<>(students);
        pageBean.setData(students);
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalRecord((int) pageInfo.getTotal());
        pageBean.setTotalPage(pageInfo.getPages());
        return pageBean;
    }
}
